package com.bort.system.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bort.system.model.Role;
import com.bort.system.model.User;
import com.bort.system.model.vo.Admin;
import com.bort.system.model.vo.VipUser;
import com.bort.system.service.RoleService;
import com.bort.system.service.UserService;
import com.bort.system.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    private Logger logger = LoggerFactory.getLogger(AdminUserController.class);

    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    @GetMapping("/list")
    public Result users(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "type",defaultValue = "3") byte type,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize)
     {
        Result result = new Result();
        Page<VipUser> pager = new Page<>();
        pager.setSize(rowSize);
        pager.setCurrent(page);
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("name",name);
        map.put("phone",phone);
        userService.selectVipUsers(pager,map);
        result.setData(pager);
        return result;
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        if (user.getId() != null && user.getId() > 0) {
            // edit
            userService.updateUser(user);
        } else {
            // add
            user.setCreateTime(new Date());
            user.setType((byte) 3);
            userService.addUser(user);
        }
        return new Result();
    }

    @PostMapping("/admin/save")
    public Result saveAdmin(@RequestBody Admin admin) {
        if (admin.getId() != null && admin.getId() != 0) {
            userService.updateAdmin(admin);
        } else {
            userService.insetAdmin(admin);
        }
        return new Result();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(@RequestParam Integer id) {
        userService.deleteUser(id);
        return new Result();
    }

    @GetMapping("/admins")
    public Result admins(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type",defaultValue = "1") byte type,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize) {
        Result result = new Result<>();
        Page<Admin> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(rowSize);
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        userService.selectByPage(pager,map);
        result.setData(pager);
        return result;
    }

    @GetMapping("/admin/load")
    public Result loadAdmins(@RequestParam(required = false,defaultValue = "1")Integer userType){
      return  userService.loadUser(userType);
    }

    @GetMapping("/roles")
    public Result<Role> roles() {
        List<Role> list = roleService.selectAll(null);
        Result res = new Result();
        res.setData(list);
        return res;
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Result upload(@RequestParam(value = "file",required = false)MultipartFile file){
        return userService.upload(file);
    }

    @RequestMapping(value = "/updatePwd",method = RequestMethod.POST)
    public Result updatePwd(@RequestBody Map<String,Object> map){
        return userService.updatePassword(map);
    }
}
